package cn.topevery.garden.wapi.controller;

import com.github.dozermapper.core.Mapper;
import hw.topevery.basis.dal.logic.SysMapInTreeLogic;
import hw.topevery.basis.dal.logic.SysMapLogic;
import hw.topevery.basis.dal.logic.SysMapTreeDefineLogic;
import hw.topevery.basis.dal.util.SysMapTreeUtil;
import hw.topevery.basis.entity.fetch.MapFetch;
import hw.topevery.basis.entity.po.SysMap;
import hw.topevery.basis.entity.po.SysMapInTree;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author code-generate
 * @date 2022-01-05
 */
@Api(tags = "")
@RestController
@RequestMapping("/map")
public class SysMapController extends BaseController {

    @Autowired
    private Mapper mapper;

    @Autowired
    SysMapLogic sysMapLogic;


    @Autowired
    private SysMapTreeDefineLogic sysMapTreeDefineLogic;

    @Autowired
    private SysMapInTreeLogic sysMapInTreeLogic;

    @Autowired
    private SysMapTreeUtil sysMapTreeUtil;


    private List<SysMap> findSysMap(List<SysMap> dataSource, String mapId) {
        List<SysMap> newmapList = new ArrayList<>();
        for (SysMap map : dataSource) {
            if (map.mapPid.equals(mapId)) {
                newmapList.add(map);
            }
        }
        return newmapList;
    }

    private List<SysMapInTree> findSysMapInTree(List<SysMapInTree> dataSource, String mapId) {
        List<SysMapInTree> newmapList = new ArrayList<>();
        for (SysMapInTree map : dataSource) {
            if (map.mapPid.equals(mapId)) {
                newmapList.add(map);
            }
        }
        return newmapList;
    }


    @ApiOperation(value = "获取区域列表查询")
    @GetMapping("/list")
    public Object getMapList(@RequestParam(name = "parentId", required = false) String parentId) {
        JsonResult<List<SysMap>> result = new JsonResult<>();
        try {
            result.data = sysMapLogic.getListMap(parentId);
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
        }
        return result;

    }

    @ApiOperation(value = "获取区域列表查询")
    @PostMapping("/search")
    public Object getMapSearch(@RequestBody MapFetch para) {

        JsonResult<DataPager<SysMap>> result = new JsonResult<>(true);
        try {
            DataPager<SysMap> mapSearch = sysMapLogic.getMapSearch(para);
            result.data = mapSearch;
        } catch (Exception ignored) {
            result.setCode(400);
            result.success = false;
            result.msg = ignored.getMessage();
        }
        return result;

    }

    @ApiOperation("根据父节点区域id获取区域树")
    @GetMapping("/getMapTree")
    public JsonResult<?> getMapTree(@RequestParam String mapId) {
        return JsonResult.ok(sysMapTreeUtil.getMapTree(mapId));
    }

}
